#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash fftw-3.3.10.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* ||  "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* ||  "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
set -e

wget -t 0 -c -P $buildpath http://fftw.org/fftw-3.3.10.tar.gz
tar xf $buildpath/fftw-3.3.10.tar.gz -C $buildpath && rm -rf $buildpath/fftw-3.3.10.tar.gz
cd $buildpath/fftw-3.3.10

./bootstrap.sh
CC=clang CXX=clang++ ./configure --prefix=$installpath/fftw-3.3.10 --enable-shared --enable-fma --enable-neon --enable-float
make -j$(nproc) && make install
cp -r $installpath/fftw-3.3.10/lib/* /usr/lib64

CC=clang CXX=clang++ ./configure --prefix=$installpath/fftw-3.3.10 --enable-shared --enable-fma --enable-neon
make -j$(nproc) && make install
cp -r $installpath/fftw-3.3.10/lib/* /usr/lib64

cat>"$installpath/fftw-3.3.10/fftw3_modulefiles"<<EOF
#%Module1.0
conflict fftw3
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set fftw3 \$pwd
setenv fftw3 \$fftw3 
prepend-path PATH \$fftw3/bin
prepend-path LD_LIBRARY_PATH \$fftw3/lib
prepend-path INCLUDE \$fftw3/include
EOF

rm -rf $buildpath/fftw-3.3.10

